#include "CatchTheGarbage.h"

CatchTheGarbage::CatchTheGarbage():Scene()
{
	GameTime = PlayTime;
	CountDownTime = CountTime;
	MaxSpeed = 3.5;
	MaxHeight = Vector2D<float>(0.0, -50);
	BananaIndex = 0;
	GameEndTime = GameEndCountDown;

	// Player 1
	Player1 = Object2D(45.0, 45.0, 45.0, true);
	Player1.setPosition(200.0, 450.0);
	Player1Jump = false;
	Player1MoveSpeed = 0.0;
	Player1JumpSpeed = Vector2D<float>(MaxHeight);
	P1Index = 0;
	P1Score = 0;
	P1State = 0;
	P1Slipping = false;

	// Player 2
	Player2 = Object2D(45.0, 45.0, 45.0, true);
	Player2.setPosition(600.0, 450.0);
	Player2Jump = false;
	Player2MoveSpeed = 0.0;
	Player2JumpSpeed = Vector2D<float>(MaxHeight);
	P2Index = 0;
	P2Score = 0;
	P2State = 0;
	P2Slipping = false;

	// Player win condition
	PlayerWins = 0;

	// Background moving animation
	BG_Moved = 0.0;
	BG_MoveLeft = true;

	// Object
	Floor = Object2D(500.0, 100.0, 500.0, true);
	FloatFloorLeft = Object2D(150.0, 20.0, 150.0, true);
	FloatFloorRight = Object2D(150.0, 20.0, 150.0, true);
	FloatFloorCenter = Object2D(500.0, 20.0, 500.0, true);	
	BackGround = Object2D(1000.0, 600.0, 0.0, true);
	SignalPage = Object2D(800.0, 600.0, 0.0, false);

	Garbage = Object2D(50.0, 50.0, 50.0, true);
	for(int x = 0; x < 5; x++)
		BonusGarbage[x] = Object2D(50.0, 50.0, 50.0, false);
	for(int y = 0; y < MaxBanana; y++)
		Banana[y] = Object2D(35.0, 35.0, 35.0, false);

	// Objects for HUD
	Time_Word = Object2D(60.0, 30.0, 0.0, false);
	Time_Tenth = Object2D(45.0, 45.0, 0.0, false);
	Time_One = Object2D(45.0, 45.0, 0.0, false);
	P1Score_Word = Object2D(60.0, 30.0, 0.0, false);
	P1Score_Tenth = Object2D(45.0, 45.0, 0.0, false);
	P1Score_One = Object2D(45.0, 45.0, 0.0, false);
	P2Score_Word = Object2D(60.0, 30.0, 0.0, false);
	P2Score_Tenth = Object2D(45.0, 45.0, 0.0, false);
	P2Score_One = Object2D(45.0, 45.0, 0.0, false);

	// Objects for points
	Point_1 = Object2D(45.0, 45.0, 0.0, false);
	Point_2 = Object2D(45.0, 45.0, 0.0, false);
		
	// Object position
	Floor.setPosition(400.0, 550.0);
	FloatFloorLeft.setPosition(100.0, 400.0);
	FloatFloorRight.setPosition(700.0, 400.0);
	FloatFloorCenter.setPosition(400.0, 220.0);
	BackGround.setPosition(400.0, 300.0);
	SignalPage.setPosition(400.0, 300.0);

	// Objects position for HUD
	Time_Word.setPosition(400.0, 20.0);
	Time_Tenth.setPosition(385.0, 55.0);
	Time_One.setPosition(415.0, 55.0);
	P1Score_Word.setPosition(200.0, 20.0);
	P1Score_Tenth.setPosition(185.0, 55.0);
	P1Score_One.setPosition(215.0, 55.0);
	P2Score_Word.setPosition(600.0, 20.0);
	P2Score_Tenth.setPosition(585.0, 55.0);
	P2Score_One.setPosition(615.0, 55.0);

	// Objects for points position
	Point_1.setPosition(-100.0, -100.0);
	Point_2.setPosition(-100.0, -100.0);

	// Random generating garbage position
	Garbage.setPosition(Randomizer::getInstance().randMax(650,150), 200.0);
	BonusGarbage[0].setPosition(Floor.getPosition().getX(), Floor.getTop() - BonusGarbage[0].getHeight() / 2);
	BonusGarbage[1].setPosition(FloatFloorLeft.getPosition().getX(), FloatFloorLeft.getTop() - BonusGarbage[1].getHeight() / 2);
	BonusGarbage[2].setPosition(FloatFloorRight.getPosition().getX(), FloatFloorRight.getTop() - BonusGarbage[2].getHeight() / 2);
	BonusGarbage[3].setPosition(FloatFloorCenter.getPosition().getX() - 100.0, FloatFloorCenter.getTop() - BonusGarbage[3].getHeight() / 2);
	BonusGarbage[4].setPosition(FloatFloorCenter.getPosition().getX() + 100.0, FloatFloorCenter.getTop() - BonusGarbage[4].getHeight() / 2);

	for(int y = 0; y < MaxBanana; y++)
		Banana[y].setPosition(-100.0, -100.0);

	// Water flow
	WaterFlow = Object2D(800.0, 50.0, 0.0, false);
	WaterFlow.setPosition(400.0, 580.0);
	WaterFlowIndex = 0;
}

void CatchTheGarbage::loadResource()
{
	// Player 1 Image
	P1Sprite[0] = new Sprite(4,1, "images/catchthegarbage/Chatacter_Tails_Standing.tga");
	P1Sprite[1] = new Sprite(4,1, "images/catchthegarbage/Character_Tails_Walking.tga");
	P1Sprite[2] = new Sprite(4,1, "images/catchthegarbage/Character_Tails_Falling.tga");

	// Player 2 Image
	P2Sprite[0] = new Sprite(4,1, "images/catchthegarbage/Character_Sonic_Standing.tga");
	P2Sprite[1] = new Sprite(4,1, "images/catchthegarbage/Character_Sonic_Walking.tga");
	P2Sprite[2] = new Sprite(4,1, "images/catchthegarbage/Character_Sonic_Falling.tga");

	// Garbage Image
	LoadTGA( &Image_Garbage[0], "images/catchthegarbage/Image_Garbage.tga", false);
	LoadTGA( &Image_Garbage[1], "images/catchthegarbage/Image_BonusGarbage.tga", false);

	// Environment Image
	LoadTGA( &Image_Environment[0], "images/catchthegarbage/Image_GrassPatch.tga", false);
	LoadTGA( &Image_Environment[1], "images/catchthegarbage/Image_Sky.tga", false);
	LoadTGA( &Image_Environment[2], "images/catchthegarbage/Image_Banana.tga", false);
	LoadTGA( &Image_Environment[3], "images/catchthegarbage/Image_Time.tga", false);
	LoadTGA( &Image_Environment[4], "images/catchthegarbage/Image_P1Score.tga", false);
	LoadTGA( &Image_Environment[5], "images/catchthegarbage/Image_P2Score.tga", false);
	LoadTGA( &Image_Environment[6], "images/catchthegarbage/Image_Plus1.tga", false);
	LoadTGA( &Image_Environment[7], "images/catchthegarbage/Image_Plus2.tga", false);

	// Water Animation Image
	LoadTGA( &Image_WaterFlow[0], "images/catchthegarbage/AnimationWater_1.tga", false);
	LoadTGA( &Image_WaterFlow[1], "images/catchthegarbage/AnimationWater_2.tga", false);
	LoadTGA( &Image_WaterFlow[2], "images/catchthegarbage/AnimationWater_3.tga", false);
	LoadTGA( &Image_WaterFlow[3], "images/catchthegarbage/AnimationWater_4.tga", false);
	LoadTGA( &Image_WaterFlow[4], "images/catchthegarbage/AnimationWater_5.tga", false);
	LoadTGA( &Image_WaterFlow[5], "images/catchthegarbage/AnimationWater_6.tga", false);
	LoadTGA( &Image_WaterFlow[6], "images/catchthegarbage/AnimationWater_7.tga", false);
	LoadTGA( &Image_WaterFlow[7], "images/catchthegarbage/AnimationWater_8.tga", false);
	LoadTGA( &Image_WaterFlow[8], "images/catchthegarbage/AnimationWater_9.tga", false);
	LoadTGA( &Image_WaterFlow[9], "images/catchthegarbage/AnimationWater_10.tga", false);	

	// Count down Image
	LoadTGA( &Image_CountDown[0], "images/catchthegarbage/CatchGarbage_Intro_1.tga", false);
	LoadTGA( &Image_CountDown[1], "images/catchthegarbage/CatchGarbage_Intro_2.tga", false);
	LoadTGA( &Image_CountDown[2], "images/catchthegarbage/CatchTheGarbage_Tutorial.tga", false);	
	LoadTGA( &Image_CountDown[3], "images/catchthegarbage/Image_Ready.tga", false);
	LoadTGA( &Image_CountDown[4], "images/catchthegarbage/Image_Go.tga", false);

	// Game end
	LoadTGA( &Image_GameEnd[0], "images/catchthegarbage/Image_GameOverDraw.tga", false);
	LoadTGA( &Image_GameEnd[1], "images/catchthegarbage/Image_GameOverP1Win.tga", false);
	LoadTGA( &Image_GameEnd[2], "images/catchthegarbage/Image_GameOverP2Win.tga", false);

	// Numbers
	Numbers[0] = new Sprite(10,1,"images/catchthegarbage/Image_Numbers.tga");
	Numbers[1] = new Sprite(10,1,"images/catchthegarbage/Image_NumbersRed.tga");
}

void CatchTheGarbage::freeResource()
{
	for(int x = 0; x < 5; x++)
		freeTexture(&Image_CountDown[x]);

	for(int x = 0; x < 3; x++)
		freeTexture(&Image_GameEnd[x]);

	for(int x = 0; x < 2; x++)
		freeTexture(&Image_Garbage[x]);

	for(int x = 0; x < 3; x++)
		freeTexture(&Image_Environment[x]);

	for(int x = 0; x < 10; x++)
		freeTexture(&Image_WaterFlow[x]);
}

void CatchTheGarbage::reset()
{
	GameTime = PlayTime;
	CountDownTime = CountTime;
	GameEndTime = GameEndCountDown;

	// Player 1
	Player1 = Object2D(30.0, 30.0, 30.0, true);
	Player1.setPosition(200.0, 450.0);
	Player1Jump = false;
	Player1MoveSpeed = 0.0;
	Player1JumpSpeed = Vector2D<float>(MaxHeight);
	P1Index = 0;
	P1Score = 0.0;
	P1State = 0;
	P1Slipping = false;

	// Player 2
	Player2 = Object2D(30.0, 30.0, 30.0, true);
	Player2.setPosition(600.0, 450.0);
	Player2Jump = false;
	Player2MoveSpeed = 0.0;
	Player2JumpSpeed = Vector2D<float>(MaxHeight);
	P2Index = 0;
	P2Score = 0;
	P2State = 0;
	P2Slipping = false;

	// Background moving animation
	BG_Moved = 0.0;
	BG_MoveLeft = true;

	WaterFlowIndex = 0;
	BananaIndex = 0;

	for(int y = 0; y < MaxBanana; y++)
		Banana[y].setPosition(-100.0, -100.0);

	// Reposition displaying of players' scores
	P1Score_Word.setPosition(200.0, 20.0);
	P1Score_Tenth.setPosition(185.0, 55.0);
	P1Score_One.setPosition(215.0, 55.0);
	P2Score_Word.setPosition(600.0, 20.0);
	P2Score_Tenth.setPosition(585.0, 55.0);
	P2Score_One.setPosition(615.0, 55.0);

	// Objects for points position
	Point_1.setPosition(-100.0, -100.0);
	Point_2.setPosition(-100.0, -100.0);

	// Random generating garbage position
	Garbage.setPosition(Randomizer::getInstance().randMax(650,150), 200.0);

	for(int x = 0; x < 5; x++)
		BonusGarbage[x].setSolid(false);
	BonusGarbage[0].setPosition(Floor.getPosition().getX(), Floor.getTop() - BonusGarbage[0].getHeight() / 2);
	BonusGarbage[1].setPosition(FloatFloorLeft.getPosition().getX(), FloatFloorLeft.getTop() - BonusGarbage[1].getHeight() / 2);
	BonusGarbage[2].setPosition(FloatFloorRight.getPosition().getX(), FloatFloorRight.getTop() - BonusGarbage[2].getHeight() / 2);
	BonusGarbage[3].setPosition(FloatFloorCenter.getPosition().getX() - 100.0, FloatFloorCenter.getTop() - BonusGarbage[3].getHeight() / 2);
	BonusGarbage[4].setPosition(FloatFloorCenter.getPosition().getX() + 100.0, FloatFloorCenter.getTop() - BonusGarbage[4].getHeight() / 2);
}

void CatchTheGarbage::draw()
{
	if(CountDownTime > 14)
	{
		SignalPage.drawPlane(Image_CountDown[0].texID);			// Display intro part 1
	}
	else if(CountDownTime > 11)
	{
		SignalPage.drawPlane(Image_CountDown[1].texID);			// Display intro part 2
	}
	else if(CountDownTime > 3)
	{
		SignalPage.drawPlane(Image_CountDown[2].texID);			// Display tutorial
	}
	else if(CountDownTime > 2)
	{
		// For YEE WEI, play ready sound
		
		SignalPage.drawPlane(Image_CountDown[3].texID);			// Display ready
	}
	else if(CountDownTime > 1)
	{
		// For YEE WEI, play go sound
	
		SignalPage.drawPlane(Image_CountDown[4].texID);			// Display go
	}

	// Game ends
	if(GameTime == 0)
	{
		SignalPage.drawPlane(Image_GameEnd[PlayerWins].texID);

		// Players' score
		int TempP1Score = P1Score;
		int TempP2Score = P2Score;

		P1Score_Word.drawPlane(Image_Environment[4].texID);
		P1Score_Tenth.drawPlane(Numbers[0],P1Score/10);
		P1Score_One.drawPlane(Numbers[0],TempP1Score%10);
		P2Score_Word.drawPlane(Image_Environment[5].texID);
		P2Score_Tenth.drawPlane(Numbers[0],P2Score/10);
		P2Score_One.drawPlane(Numbers[0],TempP2Score%10);
	}

	if(CountDownTime == 0 && GameTime != 0)
	{
		// Background
		BackGround.drawPlane(Image_Environment[1].texID);

		// HUD
		// Passing in to a integer variable so that can take the remainder when dividing like TempTime%10
		int TempTime = GameTime;
		int TempP1Score = P1Score;
		int TempP2Score = P2Score;

		// Time(HUD)
		Time_Word.drawPlane(Image_Environment[3].texID);

		// Players' score(HUD)
		P1Score_Word.drawPlane(Image_Environment[4].texID);
		P1Score_Tenth.drawPlane(Numbers[0],P1Score/10);
		P1Score_One.drawPlane(Numbers[0],TempP1Score%10);
		P2Score_Word.drawPlane(Image_Environment[5].texID);
		P2Score_Tenth.drawPlane(Numbers[0],P2Score/10);
		P2Score_One.drawPlane(Numbers[0],TempP2Score%10);
			
		// Non Bonus time
		if(GameTime > 10)
		{
			// Draw number in white
			Time_Tenth.drawPlane(Numbers[0],GameTime/10);
			Time_One.drawPlane(Numbers[0],TempTime%10);

			// Signal Points
			Point_1.drawPlane(Image_Environment[6].texID);
			// Garbage
			Garbage.drawPlane(Image_Garbage[0].texID);
		}

		// Bonus Garbage
		if(GameTime < 10)
		{
			// Draw number in red
			Time_Tenth.drawPlane(Numbers[1],GameTime/10);
			Time_One.drawPlane(Numbers[1],TempTime%10);

			Point_2.drawPlane(Image_Environment[7].texID);

			for(int x = 0; x < 5; x++)
				BonusGarbage[x].drawPlane(Image_Garbage[1].texID);
		}

		// Banana
		for(int y = 0; y < MaxBanana; y++)
			Banana[y].drawPlane(Image_Environment[2].texID);

		// Player
		Player1.drawPlane(P1Sprite[P1State], P1Index);
		Player2.drawPlane(P2Sprite[P2State], P2Index);

		// Floor
		Floor.drawPlane(Image_Environment[0].texID);
		FloatFloorLeft.drawPlane(Image_Environment[0].texID);
		FloatFloorRight.drawPlane(Image_Environment[0].texID);
		FloatFloorCenter.drawPlane(Image_Environment[0].texID);	

		// Water Plane
		WaterFlow.drawPlane(Image_WaterFlow[WaterFlowIndex].texID);
	}
}

void CatchTheGarbage::update()
{
	if(CountDownTime != 0)
	{
		// Update game time
		float TimePassed = 1 / RealTimeController::getInstance().getFPS();
		CountDownTime -= TimePassed;

		if(CountDownTime < 0)
			CountDownTime = 0;
	}

	if(CountDownTime == 0)
	{
		// Update game time
		float TimePassed = 1 / RealTimeController::getInstance().getFPS();
		GameTime -= TimePassed;

		if(GameTime < 0)
			GameTime = 0;

		// For bonus time
		if(GameTime < 10)
		{
			BonusUpdate();
		}
		BananaUpdate();
	}

	if(GameTime != 0)
	{
		// Background moving left
		if(BG_MoveLeft == true)
		{
			BackGround.move(-BackGroundMovingSpeed, 0.0);
			BG_Moved += BackGroundMovingSpeed;
		}
		// Background moving right
		else if(BG_MoveLeft == false)
		{
			BackGround.move(BackGroundMovingSpeed, 0.0);
			BG_Moved += BackGroundMovingSpeed;
		}

		// If background moved certain distance, set to another direction
		if(BG_Moved > 100.0f)
		{
			BG_MoveLeft = !BG_MoveLeft;			// Change moving direction 
			BG_Moved = 0.0f;									// Reset distance travelled
		}
		
		// Water flow animation update
		WaterFlowIndex++;
		// Update player's animation
		if(WaterFlowIndex > 9)
			WaterFlowIndex = 0;

		GarbageUpdate();
	
		Player1Update();
		
		Player2Update();

		// If AI Mode on
		if(_SinglePlayer == true)
		{
			AIUpdate();
		}

		// Display plus point picture update
		if(GameTime > 10)
		{
			Point_1.move(0.0, -5.0);
		}
		else
		{
			Point_2.move(0.0, -5.0);
		}
	}

	if(GameTime == 0)
	{
		// For YEE WEI, play cheering sound
		PlaySound("sounds/catchthegarbage/game_over.wav",NULL,SND_FILENAME|SND_ASYNC);
		P1Score_Word.setPosition(200.0, 420.0);
		P1Score_Tenth.setPosition(185.0, 455.0);
		P1Score_One.setPosition(215.0, 455.0);
		P2Score_Word.setPosition(600.0, 420.0);
		P2Score_Tenth.setPosition(585.0, 455.0);
		P2Score_One.setPosition(615.0, 455.0);

		// Update game end time
		float TimePassed = 1 / RealTimeController::getInstance().getFPS();
		GameEndTime -= TimePassed;

		// Goes back to info stage page when time's up
		if(GameEndTime < 0)
		{
			(P1Score > P2Score) ? LeaderBoard::getInstance().P1_AddScore() : LeaderBoard::getInstance().P2_AddScore();
			SceneManager::getInstance().changeScene("Score_Board");
		}
		else
		{
			// Player 1 wins
			if(P1Score > P2Score)
			{
				PlayerWins = 1;
			}
			// Player 2 wins
			else if(P1Score < P2Score)
			{
				PlayerWins = 2;
			}
			// Draws
			else if(P1Score == P2Score)
			{
				PlayerWins = 0;
			}
		}
	}
}

void CatchTheGarbage::AIUpdate()
{
	// If AI(Player2) is at the same level with Player 1
	if(Player2.getPosition().getY() == Player1.getPosition().getY())
	{
		// If Player 2 is reaching the left corner of the platform
		 if(Player2.getPosition().getX() > Floor.getLeft() && Player2.getPosition().getX() < Floor.getLeft() + 50.0)
		{
			 AIMovement('l',0,0);		// Move right side
		 }
		 // If Player 2 is reaching the right corner of the platform
		 else if(Player2.getPosition().getX() < Floor.getRight() && Player2.getPosition().getX() > Floor.getRight() - 50.0)
		{
			AIMovement('j',0,0);		// Move left side
		}

		else
		{
			 // To calculate player 1's position with player 2
			float Distance = Player2.getPosition().getX() - Player1.getPosition().getX();

			// If player 1 is at the left side
			if(Distance > 0 && Distance < 100.0)
			{	
				AIMovement('j',0,0);		// Move left side
			}

			// If player 1 is at the right side
			else if(Distance < 0 && Distance < -100.0)
			{
				AIMovement('l',0,0);		// Move right side
			}
		}
	}

	// Move right when AI(Player 2) going close to the left corner of the floating floor at the right
	else if(Player2.getPosition().getX() > FloatFloorRight.getRight() - 20.0)
	{
		AIMovement('j',0,0);
	}

	// Move right when AI(Player 2) going close to the left corner of the floating floor at the left
	else if(Player2.getPosition().getX() < FloatFloorLeft.getLeft() + 20.0)
	{
		AIMovement('l',0,0);
	}

	// If garbage is the same level with the AI(Player 2)
	else if(Garbage.getBottom() == Player2.getBottom())
	{
		// To calculate Garbage's position with player 2
		float Distance = Player2.getPosition().getX() - Garbage.getPosition().getX();

		// If garbage is the left side
		if(Distance > 0)
		{
			AIMovement('j',0,0);		// Move left side
		}

		// If garbage is the left side
		else
		{
			AIMovement('l',0,0);		// Move right side
		}
	}

	// Move right when AI(Player 2) going close to the left corner of the floor
	else if(Player2.getPosition().getX() > Floor.getLeft() && Player2.getPosition().getX() < Floor.getLeft() + 10.0)
	{
		AIMovement('l',0,0);
	}

	// Move right when AI(Player 2) going close to the right corner of the floor
	else if(Player2.getPosition().getX() < Floor.getRight() && Player2.getPosition().getX() > Floor.getRight() - 10.0)
	{
		AIMovement('j',0,0);
	}

	// If Garbage is not at the same level with AI(Player 2)
	else if(Garbage.getBottom() != Player2.getBottom())
	{
		// Jump when AI(Player 2) is at the left corner of the Floor(Object)
		if(Player2.getPosition().getX() > Floor.getLeft() && Player2.getPosition().getX() < Floor.getLeft() + 20.0)
		{		
			AIMovement('k',0,0);
			 PlaySound("sounds/catchthegarbage/jump.wav",NULL,SND_FILENAME|SND_ASYNC);
		}
		// Jump when AI(Player 2) is at the right corner of the Floor(Object)
		else if(Player2.getPosition().getX() < Floor.getRight() && Player2.getPosition().getX() > Floor.getRight() - 20.0)
		{
			AIMovement('k',0,0);
			PlaySound("sounds/catchthegarbage/jump.wav",NULL,SND_FILENAME|SND_ASYNC);
		}
	}
}

void CatchTheGarbage::Player1Update()
{
	//---------------------------------------- Player 1's update -------------------------------------------------------

	// Update player's movement on x-axis
	Player1.move(Player1MoveSpeed, 0.0);

	// Change player state to standing when not moving
	if( Player1MoveSpeed == 0 )
	{
		P1State = 0;
	}

	// Update player's animation
	if(P1Index > 2)
		P1Index = 0;

	// When player collide from the top with another
	if(Player2.Intersects(Player1) && Player2.getTop() < Player1.getTop())
	{
		// Spawn back to original position
		Player1.setPosition(200.0, 450.0);
		Player2.setPosition(600.0, 450.0);
		Player1MoveSpeed = 0.0;
		Player2MoveSpeed = 0.0;
	}

	if(Player1.Intersects(Player2))
	{
		// For YEE WEI, play hitting sound
		PlaySound("sounds/catchthegarbage/bounce.wav",NULL,SND_FILENAME|SND_ASYNC);
		float tempSpeed;

		// Changes speed when collided
		tempSpeed = Player2MoveSpeed;
		Player2MoveSpeed = Player1MoveSpeed;
		Player1MoveSpeed = tempSpeed;
	}

	// Player drops down when not collided with the floor
	Player1.move(_GRAVITY2D_);

	// When player 1 collides with floor
	if(Player1.Intersects(Floor))
	{
		if(Player1JumpSpeed.getY() > 0)
		{
			Player1Jump = false;
			Player1JumpSpeed = MaxHeight;
		}

		Player1.move(_GRAVITY2D_ * -1);		// Stop player from dropping down		
		Player1.setPosition(Player1.getPosition().getX(), Floor.getTop()-Player1.getHeight()/2);		// Set player 1 above the floor
	}

	if(Player1.Intersects(FloatFloorLeft))
	{
		if(Player1JumpSpeed.getY() > 0)
		{
			Player1Jump = false;
			Player1JumpSpeed = MaxHeight;
		}

		Player1.move(_GRAVITY2D_ * -1);
		Player1.setPosition(Player1.getPosition().getX(), FloatFloorLeft.getTop()-Player1.getHeight()/2);
	}

	if(Player1.Intersects(FloatFloorRight))
	{
		if(Player1JumpSpeed.getY() > 0)
		{
			Player1Jump = false;
			Player1JumpSpeed = MaxHeight;
		}

		Player1.move(_GRAVITY2D_ * -1);
		Player1.setPosition(Player1.getPosition().getX(), FloatFloorRight.getTop()-Player1.getHeight()/2);
	}

	if(Player1.Intersects(FloatFloorCenter))
	{
		if(Player1JumpSpeed.getY() > 0)
		{
			Player1Jump = false;
			Player1JumpSpeed = MaxHeight;
		}

		Player1.move(_GRAVITY2D_ * -1);
		Player1.setPosition(Player1.getPosition().getX(), FloatFloorCenter.getTop()-Player1.getHeight()/2);
	}

	if(Player1Jump == true)
	{
		Player1.move(Player1JumpSpeed);							// Player's y position moving up
		Player1JumpSpeed += (_GRAVITY2D_/9.81)*5;		// How much left for the player to jump
	}

	// When player drops down to the hole

	// Checks player 2 position, make sure player 1 spawns at a place where player 2 is far and not dangerous
	if( Player1.getPosition().getY() > Floor.getBottom() )
	{
		if( Player2.getPosition().getX() > Floor.getPosition().getX() )
			Player1.setPosition(Player2.getPosition().getX() - 200.0, 450.0);
		else
		{
			Player1.setPosition(Player2.getPosition().getX() + 200.0, 450.0);
		}

		Player1MoveSpeed = 0;		// Set speed to 0 again
		P1Score = 0;							// Set score to 0
	}
	//---------------------------------------- End of Player 1's update -------------------------------------------------------
}


void CatchTheGarbage::Player2Update()
{
	//---------------------------------------- Player 2's update -------------------------------------------------------

	// Update of players' speed
	Player2.move(Player2MoveSpeed, 0.0);	

	// Change player state to standing when not moving
	if( Player2MoveSpeed == 0 )
	{
		P2State = 0;
	}

	// Update player's animation
	if(P2Index > 2)
		P2Index = 0;

	// When player collide from the top with another
	if(Player1.Intersects(Player2) && Player1.getTop() < Player2.getTop())
	{
		// Spawn back to original position
		Player1.setPosition(200.0, 450.0);
		Player2.setPosition(600.0, 450.0);
		Player1MoveSpeed = 0.0;
		Player2MoveSpeed = 0.0;
	}

	// Player drops down when not collided with the floor
	Player2.move(_GRAVITY2D_);

	if(Player2.Intersects(Floor))
	{
		if(Player2JumpSpeed.getY() > 0)
		{
			Player2Jump = false;
			Player2JumpSpeed = MaxHeight;
		}
		Player2.move(_GRAVITY2D_ * -1);		// Stop player from dropping down
		Player2.setPosition(Player2.getPosition().getX(), Floor.getTop()-Player2.getHeight()/2);		// Set player 2 above the floor
	}

	if(Player2.Intersects(FloatFloorLeft))
	{
		if(Player2JumpSpeed.getY() > 0)
		{
			Player2Jump = false;
			Player2JumpSpeed = MaxHeight;
		}

		Player2.move(_GRAVITY2D_ * -1);
		Player2.setPosition(Player2.getPosition().getX(), FloatFloorLeft.getTop()-Player2.getHeight()/2);
	}

	if(Player2.Intersects(FloatFloorRight))
	{
		if(Player2JumpSpeed.getY() > 0)
		{
			Player2Jump = false;
			Player2JumpSpeed = MaxHeight;
		}

		Player2.move(_GRAVITY2D_ * -1);
		Player2.setPosition(Player2.getPosition().getX(), FloatFloorRight.getTop()-Player2.getHeight()/2);
	}

	if(Player2.Intersects(FloatFloorCenter))
	{
		if(Player2JumpSpeed.getY() > 0)
		{
			Player2Jump = false;
			Player2JumpSpeed = MaxHeight;
		}

		Player2.move(_GRAVITY2D_ * -1);
		Player2.setPosition(Player2.getPosition().getX(), FloatFloorCenter.getTop()-Player2.getHeight()/2);
	}

	if(Player2Jump == true)
	{
		Player2.move(Player2JumpSpeed);							// Player's y position moving up
		Player2JumpSpeed += (_GRAVITY2D_/9.81)*5;		// How much left for the player to jump
	}	

	// Checks player 1 position, make sure player 2 spawns at a place where player 1 is far and not dangerous
	if( Player2.getPosition().getY() > Floor.getBottom() )
	{
		if( Player1.getPosition().getX() > Floor.getPosition().getX() )
			Player2.setPosition(Player1.getPosition().getX() - 200.0, 450.0);
		else
		{
			Player2.setPosition(Player1.getPosition().getX() + 200.0, 450.0);
		}

		Player2MoveSpeed = 0;		// Set speed to 0 again
		P2Score = 0;							// Set score to 0
	}
	//---------------------------------------- End of Player 2's update -------------------------------------------------------
}
void CatchTheGarbage::GarbageUpdate()
{
	// Gravity to pull garbage down
	Garbage.move(_GRAVITY2D_);

	// Garbage collision check with the floor at the bottom
	if(Garbage.Intersects(Floor))
	{
		Garbage.move(_GRAVITY2D_ * -1);		// Stop garbage from dropping down
		Garbage.setPosition(Garbage.getPosition().getX(), Floor.getTop()-Garbage.getHeight()/2);		// Set garbage above the floor
	}
	// Garbage collision check with the left floating floor
	if(Garbage.Intersects(FloatFloorLeft))
	{
		Garbage.move(_GRAVITY2D_ * -1);		// Stop garbage from dropping down
		Garbage.setPosition(Garbage.getPosition().getX(), FloatFloorLeft.getTop()-Garbage.getHeight()/2);		// Set garbage above the floor
	}
	// Garbage collision check with the right floating floor
	if(Garbage.Intersects(FloatFloorRight))
	{
		Garbage.move(_GRAVITY2D_ * -1);		// Stop garbage from dropping down
		Garbage.setPosition(Garbage.getPosition().getX(), FloatFloorRight.getTop()-Garbage.getHeight()/2);		// Set garbage above the floor
	}
	// Garbage collision check with the center floating floor
	if(Garbage.Intersects(FloatFloorCenter))
	{
		Garbage.move(_GRAVITY2D_ * -1);		// Stop garbage from dropping down
		Garbage.setPosition(Garbage.getPosition().getX(), FloatFloorCenter.getTop()-Garbage.getHeight()/2);		// Set garbage above the floor
	}

	// When player1 gets garbage, garbage's position change to a new place
	if(Garbage.Intersects(Player1))
	{
		// For YEE WEI, play get point sound
		PlaySound("sounds/catchthegarbage/getpoint.wav",NULL,SND_FILENAME|SND_ASYNC);
		// Set plus 1 point picture position at player 1's position
		Point_1.setPosition(Player1.getPosition().getX(), Player1.getPosition().getY());

		// Garbage spawn below player 1's position when player 1 is on the center floating floor
		if(Player1.getPosition().getY() < FloatFloorCenter.getPosition().getY())
			Garbage.setPosition(Randomizer::getInstance().randMax(650,150), 400.0);	
		else
		{
			// Garbage spawn above player 2's position when player 2 is under center floating floor
			Garbage.setPosition(Randomizer::getInstance().randMax(650,150), 0.0);
		}
		P1Score += 1;

		// Banana drops down to above player 2's position
		Banana[BananaIndex].setPosition(Player2.getPosition().getX(), Player2.getPosition().getY()-200.0);
		Banana[BananaIndex].setSolid(true);
		BananaIndex++;
	}

	if(Garbage.Intersects(Player2))
	{
		// For YEE WEI, play get point sound
		PlaySound("sounds/catchthegarbage/getpoint.wav",NULL,SND_FILENAME|SND_ASYNC);
		// Set plus 1 point picture position at player 2's position
		Point_1.setPosition(Player2.getPosition().getX(), Player2.getPosition().getY());

		// Garbage spawn below player 2's position when player 2 is on the center floating floor
		if(Player2.getPosition().getY() < FloatFloorCenter.getPosition().getY())
			Garbage.setPosition(Randomizer::getInstance().randMax(650,150), 400.0);	
		{
			// Garbage spawn above player 2's position when player 2 is under center floating floor
			Garbage.setPosition(Randomizer::getInstance().randMax(650,150), 0.0);
		}
		P2Score += 1;

		// Banana drops down to above player 1's position
		Banana[BananaIndex].setPosition(Player1.getPosition().getX(), Player1.getPosition().getY()-200.0);
		Banana[BananaIndex].setSolid(true);
		BananaIndex++;
	}

	if( Garbage.getPosition().getY() > Floor.getBottom() )
	{
		Garbage.setPosition(Randomizer::getInstance().randMax(650,150), 400.0);	
	}
}
void CatchTheGarbage::BananaUpdate()
{
	// Set the array back to the first banana when it reaches max
	if(BananaIndex == MaxBanana)
		BananaIndex = 0;

	for(int z = 0; z < MaxBanana; z++)
	{
		if(Banana[z].getSolid() == true)
		{
			// Gravity pulling banana down
			Banana[z].move(_GRAVITY2D_);

			// Banana collision check with the floor at the bottom
			if(Banana[z].Intersects(Floor))
			{
				Banana[z].move(_GRAVITY2D_ * -1);		// Stop Banana from dropping down
				Banana[z].setPosition(Banana[z].getPosition().getX(), Floor.getTop()-Banana[z].getHeight()/2);		// Set Banana above the floor
			}
			// Banana collision check with the left floating floor
			if(Banana[z].Intersects(FloatFloorLeft))
			{
				Banana[z].move(_GRAVITY2D_ * -1);		// Stop Banana from dropping down
				Banana[z].setPosition(Banana[z].getPosition().getX(), FloatFloorLeft.getTop()-Banana[z].getHeight()/2);		// Set Banana above the floor
			}
			// Banana collision check with the right floating floor
			if(Banana[z].Intersects(FloatFloorRight))
			{
				Banana[z].move(_GRAVITY2D_ * -1);		// Stop Banana from dropping down
				Banana[z].setPosition(Banana[z].getPosition().getX(), FloatFloorRight.getTop()-Banana[z].getHeight()/2);		// Set Banana above the floor
			}
			// Banana collision check with the center floating floor
			if(Banana[z].Intersects(FloatFloorCenter))
			{
				Banana[z].move(_GRAVITY2D_ * -1);		// Stop Banana from dropping down
				Banana[z].setPosition(Banana[z].getPosition().getX(), FloatFloorCenter.getTop()-Banana[z].getHeight()/2);		// Set Banana above the floor
			}
			// Banana collision check with Player 1
			if(Banana[z].Intersects(Player1))
			{
				// For YEE WEI, play falling down sound
				PlaySound("sounds/catchthegarbage/slip.wav",NULL,SND_FILENAME|SND_ASYNC);
				// Set banana off the screen
				Banana[z].setSolid(false);
				Banana[z].setPosition(-100.0, -100.0);

				// Player 1 state changes to falling
				P1State = 2;
				P1Slipping = true;

				// Adding player 1 speed
				if(Player1MoveSpeed>0)
				{
					Player1MoveSpeed += SlipSpeed;
				}
				else
				{
					Player1MoveSpeed -= SlipSpeed;
				}
			}
			else
			{
				P1Slipping = false;
			}
			// Banana collision check with Player 1
			if(Banana[z].Intersects(Player2))
			{
				// For YEE WEI, play falling down sound
                PlaySound("sounds/catchthegarbage/slip.wav",NULL,SND_FILENAME|SND_ASYNC);
				// Set banana off the screen
				Banana[z].setSolid(false);
				Banana[z].setPosition(-100.0, -100.0);

				// Player 2 state changes to falling
				P2State = 2;
				P2Slipping = true;

				if(Player2MoveSpeed>0)
				{
					Player2MoveSpeed += SlipSpeed;
				}
				else
				{
					Player2MoveSpeed -= SlipSpeed;
				}
			}
		}
	}
}
void CatchTheGarbage::BonusUpdate()
{
	// Set normal garbage off the screen
	if(Garbage.getSolid() == true)
	{
		Garbage.setPosition(-100, -100.0);
		Garbage.setSolid(false);
	}
	// Set Bonus Garbage to solid for players to collect
	for(int x = 0; x < 5; x++)
	{
		if(BonusGarbage[x].getSolid() == false)
			BonusGarbage[x].setSolid(true);

		if(Player1.Intersects(BonusGarbage[x]))
		{
			// For YEE WEI, play get bonus sound
            PlaySound("sounds/catchthegarbage/DingLing.wav",NULL,SND_FILENAME|SND_ASYNC);
			// Set plus 2 points picture position at player 2's position
			Point_2.setPosition(Player1.getPosition().getX(), Player1.getPosition().getY());

			// Player 1 gets two 2 points
			P1Score += 2;
			BonusGarbage[x].setPosition(-100,0.0);				// Set normal garbage off the screen
		}
		else if(Player2.Intersects(BonusGarbage[x]))
		{
			// For YEE WEI, play get bonus sound
            PlaySound("sounds/catchthegarbage/DingLing.wav",NULL,SND_FILENAME|SND_ASYNC);
			// Set plus 2 points picture position at player 2's position
			Point_2.setPosition(Player2.getPosition().getX(), Player2.getPosition().getY());

			// Player 2 gets two 2 points
			P2Score += 2;
			BonusGarbage[x].setPosition(-100,0.0);				// Set normal garbage off the screen
		}
	}
}
void CatchTheGarbage::handleKeyboard(int key, int x, int y)
{
	switch ( key ) {
	case 27 : glutLeaveMainLoop();
	break;

	default : break;
	}
}
void CatchTheGarbage::handleKeyboardUp(int key, int x, int y)
{
	if(key == 27)
	{
		glutLeaveMainLoop();
	}

	// Player 1 move left
	if(key == 'a' || key == 'A')
	{
		P1State = 1;		// Player 1 change to walking state
		P1Index++;			// Player 1 Index update for animation
		if( Player1MoveSpeed != -MaxSpeed )
		{
			Player1MoveSpeed -= 0.5;
			Player1.faceLeft();
		}
	}

	// Player 1 move right
	if(key == 'd' || key == 'D')
	{
		P1State = 1;		// Player 1 change to walking state
		P1Index++;			// Player 1 Index update for animation
		if( Player1MoveSpeed != MaxSpeed )
		{
			Player1MoveSpeed += 0.5;
			Player1.faceRight();			
		}
	}

	// Player 1 jump
	if(key == 's' || key == 'S')
	{
		if(Player1Jump == false)
		{
			// For YEE WEI, play jump sound
            PlaySound("sounds/catchthegarbage/jump.wav",NULL,SND_FILENAME|SND_ASYNC);			
			Player1Jump = true;
		}
	}

	// If AI Mode not on
	if(_SinglePlayer == false)
	{
		// Player 2 move left
		if(key == 'j' || key == 'J')
		{
			P2State = 1;		// Player 2 change to walking state
			P2Index++;			// Player 2 Index update for animation
			if( Player2MoveSpeed != -MaxSpeed )
			{
				Player2MoveSpeed -= 0.5;
				Player2.faceLeft();
			}
		}

		// Player 2 move right
		if(key == 'l' || key == 'L')
		{
			P2State = 1;		// Player 2 change to walking state
			P2Index++;			// Player 2 Index update for animation
			if( Player2MoveSpeed != MaxSpeed )
			{
				Player2MoveSpeed += 0.5;
				Player2.faceRight();
			}
		}

		// Player 2 jump
		if(key == 'k' || key == 'K')
		{
			if(Player2Jump == false)
			{
				// For YEE WEI, play jump sound
				PlaySound("sounds/catchthegarbage/jump.wav",NULL,SND_FILENAME|SND_ASYNC);
				Player2Jump = true;
			}
		}
	}

	// Skip to start the game
	if(key == ' ' && CountDownTime != 0)
	{
		PlaySound("sounds/games/go.wav", NULL,SND_ASYNC);
		CountDownTime = 3;
	}
}

void CatchTheGarbage::AIMovement(int key, int x, int y)
{
	// AI move left
	if(key == 'j' || key == 'J')
	{
		P2State = 1;		// Player 2 change to walking state
		P2Index++;			// Player 2 Index update for animation
		if( Player2MoveSpeed != -MaxSpeed )
		{
			Player2MoveSpeed -= 0.5;
			Player2.faceLeft();
		}
	}

	// AI move right
	if(key == 'l' || key == 'L')
	{
		P2State = 1;		// Player 2 change to walking state
		P2Index++;			// Player 2 Index update for animation
		if( Player2MoveSpeed != MaxSpeed )
		{
			Player2MoveSpeed += 0.5;
			Player2.faceRight();
		}
	}

	// AI jump
	if(key == 'k' || key == 'K')
	{
		if(Player2Jump == false)
		{
			// For YEE WEI, play jump sound

			Player2Jump = true;
		}
	}

	// Skip to start the game
	if(key == ' ' && CountDownTime != 0)
	{
		CountDownTime = 3;
	}
}

void CatchTheGarbage::handleKeyboardSpecial(int key, int x, int y)
{
}

void CatchTheGarbage::getMid(int &x, int &y)
{
}

void CatchTheGarbage::handleMouseClick(int button, int state, int x, int y)
{
	switch( button )
	{
	case GLUT_LEFT_BUTTON :
		

		break;
	case GLUT_RIGHT_BUTTON : 
		
		break;
	case GLUT_MIDDLE_BUTTON : break;
	}
}
void CatchTheGarbage::handleMouseMove(int x, int y)
{
}